<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/29
 * Time: 14:58
 */
require 'vendor/autoload.php';
require 'connect.php';
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use Slim\PDO\Database;
use Slim\PDO\Statement;
use Slim\PDO\Statement\SelectStatement;
$app = new \Slim\App();



$app->get('/getLorry1',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $openid=$request->getParam('openid');
    $database=localhost();
    $selectStatement = $database->select()
        ->from('lorry')
        ->where('openid','=',$openid);
    $stmt = $selectStatement->execute();
    $data1= $stmt->fetch();
    return $response->withJson(array("result" => "0", "desc" => "success",'lorry'=>$data1));
});

$app->options('/addLorry',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $response=$response->withAddedHeader("Access-Control-Allow-Methods", "POST");
    return $response;
});

$app->post('/addLorry',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $database=localhost();
    $body = $request->getBody();
    $body=json_decode($body);
    $openid=$body->openid;
    $array=array();
    foreach($body as $key=>$value){
        $array[$key]=$value;
    }
    $selectStatement = $database->select()
    ->from('lorry')
    ->where('openid','=',$openid);
    $stmt = $selectStatement->execute();
    $data1= $stmt->fetch();
    if($data1!=null){
        $updateStatement = $database->update($array)
            ->table('lorry')
            ->where('openid','=',$openid);
        $affectedRows = $updateStatement->execute();
    }else{
        $insertStatement = $database->insert(array_keys($array))
        ->into('lorry')
        ->values(array_values($array));
    $insertId = $insertStatement->execute(false);
    }
    return $response->withJson(array("result"=>"0","desc"=>"添加成功"));
});

$app->options('/addpic',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $response=$response->withAddedHeader("Access-Control-Allow-Methods", "POST");
    return $response;
});

$app->post('/addpic',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $database=localhost();
    $time1=time();
//    $name= $_FILES["file"]["name"];
   $file_true_name=$_FILES['file']['name'];  
    $move_to_file=substr($file_true_name,strrpos($file_true_name,"."));
    $name1=iconv("UTF-8","gb2312", $move_to_file);
    $name1=$time1.$name1;
    move_uploaded_file($_FILES["file"]["tmp_name"], '/files/lorry/'.$name1);
    $order_t_p= 'http://files.yfchina56.com:8000/lorry/'.$time1.$move_to_file.'';
    return $response->withJson(array("result"=>"0","path"=>$order_t_p,"name"=>$move_to_file));
});

$app->get('/getLorrys1',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $type_id=$request->getParam('type_id');
    $database=localhost();
    $selectStatement = $database->select()
        ->from('lorry')
        ->where('type_id','=',$type_id);
    $stmt = $selectStatement->execute();
    $data1= $stmt->fetch();
    return $response->withJson(array("result" => "0", "desc" => "success",'lorrys'=>$data1));
});

$app->get('/getLorrys2',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $type_id=$request->getParam('type_id');
    $database=localhost();
    $selectStatement = $database->select()
        ->from('lorry')
        ->where('type_id','=',$type_id);
    $stmt = $selectStatement->execute();
    $data1= $stmt->fetch();
    return $response->withJson(array("result" => "0", "desc" => "success",'lorrys'=>$data1));
});

$app->run();

function localhost()
{
    return connect();
}


?>